<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"> 
        <header class="header">
            <h1>小黑记事本</h1>
            <input v-model="todoName" type="text" placeholder="请输入要记事" class="new-todo"></input>
            <button class="add" @click="add">添加任务</button> 
        </header>
        <!-- 列表区域 -->
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item, index) in list" :key="item.id">
                    <div class="view">
                        <span class="index">{{index + 1}}.</span><label>{{item.name}}</label>
                        <button @click="del(item.id)" class="destroy">删除</button>
                    </div>  
                </li>
                
            </ul>
        </section>
        <!-- 统计和清空区域 -->
         <footer class="footer" v-show="list.length > 0">
            <span class="todo-count">合 计：<strong>{{list.length}}</strong></span>
            <button @click="clear" class="clear-completed">清空任务</button>
        </footer>
        
    </div>
    
 
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                todoName: '',
                list: [
                    {id: 1, name: '跑步5公里'},
                    {id: 2, name: '跳绳200次'},
                    {id: 3, name: '游泳3公里'},
                    {id: 4, name: '骑车20公里'}
                ]
            },
            methods: {
                del(id) {  
                    this.list = this.list.filter(item => item.id !== id)
                },
                add() {
                    if (this.todoName.trim() === '') {
                        alert('请输入任务名称')
                        return
                    }
                    this.list.unshift({
                        id: this.list.length + 1,
                        name: this.todoName
                    })
                    this.todoName = ''
                },
                clear() {
                    this.list = []
                }
            }
        })
    </script>
</body>
</html>